﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using EIS.ClientUtility;
using EIS.DatabaseHelper;
using EIS.Obj;
using SelectionMode = Janus.Windows.GridEX.SelectionMode;

namespace EIS.ConfigViews
{
    public partial class MaintainSapConnection : Form
    {
        public MaintainSapConnection()
        {
            InitializeComponent();
        }

        private void logonView1_Load(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
 
            DataSet ds = (DataSet)gridSAPConnection.DataSource;
            ds.AcceptChanges();

            DataSet dsToSave = new DataSet();

            dsToSave = ds.Clone();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (
                    (dr["SAPID"] == null) ||
                    (dr["SAPID"].ToString().Trim() == string.Empty) ||
                    (dr["SAPClient"] == null) ||
                    (dr["SAPClient"].ToString().Trim() == string.Empty) ||
                    (dr["SAPUser"] == null) ||
                    (dr["SAPUser"].ToString().Trim() == string.Empty) ||
                    (dr["SAPPassword"] == null) ||
                    (dr["SAPPassword"].ToString().Trim() == string.Empty)
                    )
                {
                    ShowErrorMessageBox.Show("Please input in all four fields for each SAP connection");
                    return;
                }
                dsToSave.Tables[0].ImportRow(dr);

            }

            foreach (DataRow dr in dsToSave.Tables[0].Rows)
            {
                 dr["UserID"] = ClientConfig.Instance.LogonInfo.UserName;
                 dr["SAPPassword"] = this.Encode(dr["SAPPassword"].ToString());
            }

            if ( dsToSave.Tables[0].Rows.Count == 0)
            {
                var dr = dsToSave.Tables[0].NewRow();
                dr["UserID"] = ClientConfig.Instance.LogonInfo.UserName;
                dsToSave.Tables[0].Rows.Add(dr);
                new DBHandler().DeleteRecords(EISConstant.SAPConnectionTableName, dsToSave.Tables[0]);
            
            }
            else
            {
                new DBHandler().UpdateRecords(EISConstant.SAPConnectionTableName, dsToSave.Tables[0]);
           
            }
            Close();
        }

        private void MaintainSapConnection_Load(object sender, EventArgs e)
        {
            var dbHandler = new DBHandler();
            this.tbCurrentUser.Text = ClientConfig.Instance.LogonInfo.UserName;
            DataSet ds = dbHandler.SelectSimple(EISConstant.SAPConnectionTableName);
            var dsToShow = ds.Clone();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr["UserID"].Equals(ClientConfig.Instance.LogonInfo.UserName))
                {
                    dr["SAPPassword"] = this.Decode(dr["SAPPassword"].ToString());
                    dsToShow.Tables[0].ImportRow(dr);
                }
            }

            gridSAPConnection.DataSource = dsToShow;
            gridSAPConnection.DataMember = dsToShow.Tables[0].TableName;
            gridSAPConnection.SelectionMode = SelectionMode.MultipleSelection;
            
        }

        private void gridSAPConnection_FormattingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
        {


        }

        private string Encode(string toEncode)
        {

            byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(toEncode);

            string returnValue = Convert.ToBase64String(toEncodeAsBytes);

            return returnValue;

        }

        private string Decode(string encodedData)
        {

            byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);

            string returnValue = ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);

            return returnValue;

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Janus.Windows.GridEX.GridEXRow[] drew = gridSAPConnection.GetCheckedRows();
            for (int i = 0; i < drew.Length; i++)
            {
                drew[i].Delete();
            }
            
        }


    }
}
